/*
 * lora.c
 *
 *  Created on: Jul 14, 2024
 *      Author: yuxuan
 */

// lora 0.8秒 125字节
#include "main.h"
#include "lora.h"
//#include "sys.h"
//#include "delay.h"
#include "usart.h"
#include <string.h>
#include <stdio.h>
#include "Bsp_uart.h"
#include "crc16.h"



#if LORA
/*
	uint8_t Air_rate;		//空中速率
	uint8_t pwr;			//发射功率
	uint8_t output_power;	//发射频率
	uint8_t recive_power;	//接收频率
	uint16_t local_addr;	//定点传输地址
*/
//初始化命令
uint8_t data_cmd[]={0x50,0x03,0x00,0x34,0x00,0x03,0x49,0x84};//读取三轴加速度指令


//设备参数初始化(具体设备参数见lora.h定义)
_LoRa_CFG LoRa_CFG=
{
	.Air_rate = LoRa_Air_rate,       	//空中速率
	.pwr = LoRa_pwr, 					//发射功率
	.output_power = LoRa_output_power,	//发射频率
	.recive_power = LoRa_recive_power,	//接收频率
	.local_addr = LoRa_local_addr		//定点传输地址
};






//配置loRa的参数
void LoRa_configuration(void)
{
	uint8_t sendbuf[20]; 			//AT指令缓存
	const char*config_cmd = "+++";  //进入配置指令
	const char*save_cmd = "AT&W";   //保存设置指令
	const char*restart_cmd = "AT&R";   //保存设置指令
	#if 1
		static 	uint8_t conf_step = 7;
		while (conf_step)
		{
			switch (conf_step)
			{
				case 0x07://先进入配置模式
				{
					Send_Data(&huart5,(uint8_t*)config_cmd,strlen(config_cmd));//正常来说响应OK才能进行配置
					timer_count=1;
					//Lora超时标志开启
					break;
				}
				case 0x06://设置空中传输速率
				{
					sprintf((char*)sendbuf,"AT+AIRSPEED=%d",LoRa_CFG.Air_rate);
					Send_Data(&huart5,(uint8_t*)sendbuf,strlen(sendbuf));
					break;
				}
				case 0x05://设置发射功率
				{
					sprintf((char*)sendbuf,"AT+POWER=%d",LoRa_CFG.pwr);
					Send_Data(&huart5,(uint8_t*)sendbuf,strlen(sendbuf));
					break;
				}
				case 0x04://设置发射频率
				{
					sprintf((char*)sendbuf,"AT+TXFREQ=%d",LoRa_CFG.output_power);
					Send_Data(&huart5,(uint8_t*)sendbuf,strlen(sendbuf));//(响应不在这写)
					break;
				}
				case 0x03://配置接收频率
				{
					sprintf((char*)sendbuf,"AT+RXFREQ=%d",LoRa_CFG.recive_power);
					Send_Data(&huart5,(uint8_t*)sendbuf,strlen(sendbuf));//(响应不在这写)
					break;
				}
				case 0x02://保存参数并重启
				{
					Send_Data(&huart5,(uint8_t*)save_cmd,strlen(save_cmd));//正常来说响应OK才能进行配置
					break;
				}
				case 0x01://保存参数并重启
				{
					Send_Data(&huart5,(uint8_t*)restart_cmd,strlen(restart_cmd));//正常来说响应OK才能进行配置
					break;
				}
				default:
					break;

			}
			while(timer_count > 0 && timer_count < 500)
			{
				if (lora_rx_flag == 1)
				{
					lora_rx_flag = 0;
					timer_count = 1;//重新开启计时
					conf_step--;
					break;
				}
			}
			if(timeout_flag)
			{
				timeout_flag=0;
				Send_Data(&huart3,(uint8_t*)"error",strlen("error"));//正常来说响应OK才能进行配置
				break;//跳出大循环
			}
		}

	#endif
	timer_count =0;
}

//发送采集数据指令
//测距传感器
void LoRa_send(void)
{
	if ((lora_rx_flag == 1) && (lora_rx_len > 7) && (timer_count < 50))
	{
		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);//数据检查
		//Send_Data(&huart3, lora_rx_buffer, lora_rx_len);//数据发回网关
		lora_rx_flag = 0;//标志位清一下
		timer_count=0;
	}
	//读取测量距离
	uint8_t data_cmd[]={0x01,0x03,0x00,0x0F,0x00,0x02,0xF4,0x08};
	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
	timer_count=1;//开始计时

//	while(timer_count > 0 && timer_count < 60)
//	{
//		if(lora_rx_flag)//收到返回的数据
//		{
//			lora_rx_flag = 0;//标志位清一下
//			lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);//数据检查
//			Send_Data(&huart3, lora_rx_buffer, lora_rx_len);//数据发回网关
//			timer_count=0;
//			break;
//		}
//	}
}




//数据CRC校验 并将收到的数据发回网关
//数据的具体解析在网关进行，32只需要将收到的数据发给网关即可
void lora_receivedata_CRC(uint8_t *rx_data,uint8_t len)
{
//	 char buffer[20];
//	uint16_t m_crc = 0,c_crc = 0;//用于存储计算出的CRC和接收到的CRC值的变量
	if((rx_data[0]==0x01)&&(rx_data[2]==0x04))//测距数据
	{
		Send_Data(&huart3, rx_data, len);//数据发回网关
//		if((rx_data[5]==0x01))//测量故障，0105H故障码超出量程（有故障考虑不在这里分析，直接将数据传给网关好了，在网关里分析）
//		{
//			//break;
//		}else
//		{
//			m_crc = Computer_CRC16(rx_data,len - 2);		 //计算CRC
//			c_crc = (*(rx_data + len - 2)<<8)|(*(rx_data + len - 1));//CRC校验值  倒数1 2位
//			if(m_crc == c_crc)
//			{
//				sprintf(buffer, "rx_data:%u", (unsigned int)rx_data);
//				printf("%s\n", buffer);//把收到的数据打印出来
//
//			}
//		}
	}
}




/* 计数器清0
 */
uint8_t QS96_clean_step = 7;
uint8_t QS96_clean_send_step = 1;//
void QS96_clean(void)
{
	if(QS96_clean_step)
	{
		if(timer_count>2)
		{
			QS96_clean_send_step = 1;//继续发送
			timer_count = 1;
		}

		if(QS96_clean_send_step)
		{
			switch (QS96_clean_step)
			{
				case 0x07://清计数器1的上排
				{
					uint8_t clean_cmd[]={0x01,0x06,0x00,0x00,0x00,0x01,0x48,0x0A};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x06://清计数器2的上排
				{
					uint8_t clean_cmd[]={0x02,0x06,0x00,0x00,0x00,0x01,0x48,0x39};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					break;
				}
				case 0x05://清计数器3的上排
				{
					uint8_t clean_cmd[]={0x03,0x06,0x00,0x00,0x00,0x01,0x49,0xE8};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					break;
				}
				case 0x04://清计数器4的上排
				{
					uint8_t clean_cmd[]={0x04,0x06,0x00,0x00,0x00,0x01,0x48,0x5F};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					break;
				}
				case 0x03://清计数器5的上排
				{
					uint8_t clean_cmd[]={0x05,0x06,0x00,0x00,0x00,0x01,0x49,0x8E};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					break;
				}
				case 0x02://清计数器6的上排
				{
					uint8_t clean_cmd[]={0x06,0x06,0x00,0x00,0x00,0x01,0x49,0xBD};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					break;
				}
				case 0x01://清计数器7的上排
				{
					uint8_t clean_cmd[]={0x07,0x06,0x00,0x00,0x00,0x01,0x48,0x6C};
					Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
					QS96_clean_flag = 0;
					break;
				}
				default:
					break;

			}
			QS96_clean_step--;
			QS96_clean_send_step= 0;
	//		HAL_Delay(2);

		}
	}
}

///*
// * 读取计数器数据
// */
//uint8_t QS96_collect_step = 4;
//void QS96_getdata(void)
//{
////    uint8_t failed_steps[4] = {0};  // 用于记录失败的步骤
////    uint8_t failed_count = 0;
////    uint8_t resend_flag = 0;  // 标记是否需要重发失败的指令
//
//    if(QS96_collect_step)
//    {
//        switch (QS96_collect_step)
//        {
//            case 0x04: // 读计数器1的数据
//            {
//                uint8_t getdata_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xC9};
//                Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//                timer_count = 1;
//                break;
//            }
//            case 0x03: // 读计数器2的数据
//            {
//                uint8_t getdata_cmd[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xFA};
//                Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//                break;
//            }
//            case 0x02: // 读计数器3的数据
//            {
//                uint8_t getdata_cmd[] = {0x03, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x2B};
//                Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//                break;
//            }
//            case 0x01: // 读计数器4的数据
//            {
//                uint8_t getdata_cmd[] = {0x04, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0x9C};
//                Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//                QS96_getdata_flag = 0;
//                break;
//            }
//            default:
//                break;
//        }
//
////        QS96_collect_step--;
////        resend_flag = 0;
//        while (timer_count > 10)
//        {
//        	 QS96_collect_step--;
//        	 timer_count = 1;
//        }
////
//        if ((lora_rx_flag == 1) && (lora_rx_len > 10))
//          {
//              if ((lora_rx_buffer[1] == 0x03) && (lora_rx_buffer[2] == 0x0A))
//              {
//                  Send_Data(&huart3, (uint8_t*)lora_rx_buffer, lora_rx_len);
//              }
//              lora_rx_flag = 0;
//        //        HAL_Delay(20);
//        //                break;
//          }
//    }
//
//
////
////        if (timer_count >= 100) // 如果超时
////        {
////            failed_steps[failed_count] = QS96_collect_step; // 记录失败的步骤
////        	failed_count++;
////            QS96_collect_step--;
////        }
//
//    }
//
////    timer_count = 0;
////}

/*
 * 读取计数器数据
 */
uint8_t QS96_collect_step = 8;
uint8_t QS96_collect_send_flag = 1;//
void QS96_getdata(void)
{
	static uint8_t retry_count = 0;
    if(QS96_collect_step)
    {
        if ((lora_rx_flag == 1) && (lora_rx_len > 10) && (timer_count < 50))
        {
			timer_count = 0;//停止计时
            if ((lora_rx_buffer[1] == 0x03) && (lora_rx_buffer[2] == 0x0A))
            {
//            	if((QS96_collect_step == 0x07)||(QS96_collect_step == 0x05)||(QS96_collect_step == 0x03))
//            	{
//            		HAL_Delay(10);
//            	}
            	HAL_Delay(10);
               Send_Data(&huart3, (uint8_t*)lora_rx_buffer, lora_rx_len);
            }
            lora_rx_flag = 0;
            QS96_collect_step--;
            if(QS96_collect_step == 0)
            {
            	QS96_getdata_flag = 0;
            }
			QS96_collect_send_flag =1;//继续发送
			retry_count =0;
        }
		else if (timer_count >= 50) // 如果超时
        {
			timer_count = 0;//停止计时
			retry_count++;//增加重发次数
			if(retry_count >= 2)
			{
				retry_count = 0;
				QS96_collect_step--;//重发上一次

				//如果最后一个计数器收不到数据
				if(QS96_collect_step == 0)
				{
					QS96_getdata_flag = 0;
				}
			}
			QS96_collect_send_flag = 1;//重送
        }

        if (QS96_collect_send_flag)
		{
			switch (QS96_collect_step)
			{
				case 0x08: // 读计数器1的数据
				{
					uint8_t getdata_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xC9};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
				    //QS96_collect_step = 2;//debug
					break;
				}
				case 0x07: // 读计数器2的数据
				{
					uint8_t getdata_cmd[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xFA};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x06: // 读计数器3的数据
				{
					uint8_t getdata_cmd[] = {0x03, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x2B};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x05: // 读计数器4的数据
				{
					uint8_t getdata_cmd[] = {0x04, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0x9C};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x04: // 读计数器5的数据
				{
					uint8_t getdata_cmd[] = {0x05, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x4D};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x03: // 读计数器6的数据
				{
					uint8_t getdata_cmd[] = {0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x7E};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x02: // 读计数器7的数据
				{
					uint8_t getdata_cmd[] = {0x07, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xAF};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x01: // 读计数器8的数据
				{
					uint8_t getdata_cmd[] = {0x08, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0x50};
					Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
					timer_count = 1;//计时开始
					break;
				}
				default:
					break;
			}
			//QS96_collect_step--;
            QS96_collect_send_flag = 0;
		}
    }
}

/*
 * 清除单个寄存器的值
 */
void QS96_single_clean(void)
{
//	switch (connent_rx_buffer[2])
//	{
//		case 0x01://清除计数器1
//		{
//			uint8_t clean_cmd[]={0x01,0x06,0x00,0x00,0x00,0x01,0x48,0x0A};
//			break;
//		}
//		case 0x02://清除计数器2
//		{
//			uint8_t clean_cmd[]={0x02,0x06,0x00,0x00,0x00,0x01,0x48,0x39};
//			break;
//		}
//		case 0x03://清除计数器3
//		{
//			uint8_t clean_cmd[]={0x03,0x06,0x00,0x00,0x00,0x01,0x49,0xE8};
//			break;
//		}
//		case 0x04://清除计数器4
//		{
//			uint8_t clean_cmd[]={0x04,0x06,0x00,0x00,0x00,0x01,0x48,0x5F};
//			break;
//		}
//		Send_Data(&huart5,(uint8_t*)clean_cmd,sizeof(clean_cmd));
//	}
}

/*
 * 获取单个计数器的值
 */
void QS96_get_single_data(void)
{
//	if ((lora_rx_flag == 1) && (lora_rx_len > 10) && (timer_count < 50))
//	{
//		timer_count = 0;//停止计时
//		if ((lora_rx_buffer[1] == 0x03) && (lora_rx_buffer[2] == 0x0A))
//		{
//		   Send_Data(&huart3, (uint8_t*)lora_rx_buffer, lora_rx_len);
//		}
//		lora_rx_flag = 0;
//	}
//	else if (timer_count >= 50) // 如果超时
//	{
//		timer_count = 0;//停止计时
//		retry_count++;//增加重发次数
//		if(retry_count >= 2)
//		{
//
//		}
//	}
//
//	switch (connent_rx_buffer[2])
//	{
//		case 0x01: // 读计数器1的数据
//		{
//			uint8_t getdata_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xC9};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			//QS96_collect_step = 2;//debug
//			break;
//		}
//		case 0x02: // 读计数器2的数据
//		{
//			uint8_t getdata_cmd[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xFA};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x03: // 读计数器3的数据
//		{
//			uint8_t getdata_cmd[] = {0x03, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x2B};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x04: // 读计数器4的数据
//		{
//			uint8_t getdata_cmd[] = {0x04, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0x9C};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x05: // 读计数器5的数据
//		{
//			uint8_t getdata_cmd[] = {0x05, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x4D};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x06: // 读计数器6的数据
//		{
//			uint8_t getdata_cmd[] = {0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x84, 0x7E};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x07: // 读计数器7的数据
//		{
//			uint8_t getdata_cmd[] = {0x07, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0xAF};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		case 0x08: // 读计数器8的数据
//		{
//			uint8_t getdata_cmd[] = {0x08, 0x03, 0x00, 0x00, 0x00, 0x05, 0x85, 0x50};
//			Send_Data(&huart5, (uint8_t*)getdata_cmd, sizeof(getdata_cmd));
//			timer_count = 1;//计时开始
//			break;
//		}
//		default:
//			break;
//	}

}


#endif
